package com.ruoyi.secondvitim.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.secondvitim.domain.*;
import com.ruoyi.secondvitim.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 消息controller
 * */
@RestController()
@RequestMapping("/secondVitim/message")
public class ApiMessageController extends BaseController {

    @Autowired
    private IVitimMessageService vitimMessageService;

    @GetMapping("/list")
    public TableDataInfo list(VitimMessage vitimMessage)
    {
        startPage();
        List<VitimMessage> list = vitimMessageService.selectVitimMessageList(vitimMessage);
        return getDataTable(list);
    }

    @Log(title = "保存推送消息", businessType = BusinessType.INSERT)
    @PostMapping("/send")
    public AjaxResult add(@RequestBody VitimMessage message)
    {
        //验证必填字段
        if (message.getSenderId() == null ||
                message.getReceiverId() == null ||
                StringUtils.isEmpty(message.getContent())) {
            return AjaxResult.error("发送者ID、接收者ID和消息内容不能为空");
        }

        // 设置默认值
        if (StringUtils.isEmpty(message.getSenderType())) {
            message.setSenderType("user");
        }
        if (StringUtils.isEmpty(message.getReceiverType())) {
            message.setReceiverType("nurse");
        }
        message.setIsRead("0"); // 默认未读
        // 保存消息
        int result = vitimMessageService.insertVitimMessage(message);

        if (result > 0) {
            // 返回保存后的消息（包含生成的ID）
            VitimMessage savedMessage = vitimMessageService.selectVitimMessageById(message.getId());
            return AjaxResult.success(savedMessage);
        }

        return AjaxResult.error("发送消息失败");
    }

    /**
     * 获取未读消息数量
     */
    @GetMapping("/unreadCount")
    public AjaxResult getUnreadCount(int userId) {
        int count = vitimMessageService.getUnreadCount(userId);
        return AjaxResult.success(count);
    }

    /**
     * 标记消息为已读
     */
    @PutMapping("/read")
    public AjaxResult markAsRead(@RequestBody MessageReadDTO dto) {
        if (dto.getMessageIds() == null || dto.getMessageIds().length == 0) {
            return AjaxResult.error("消息ID列表不能为空");
        }

        int result = vitimMessageService.markMessagesAsRead(dto.getMessageIds());

        if (result > 0) {
            return AjaxResult.success();
        }

        return AjaxResult.error("标记失败");
    }

    /**
     * 获取会话列表
     */
    @GetMapping("/conversations")
    public AjaxResult getConversations(int userId) {
        List<VitimMessage> conversations = vitimMessageService.getConversationList(userId);
        return AjaxResult.success(conversations);
    }

    /**
     * 获取消息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(vitimMessageService.selectVitimMessageById(id));
    }

    /**
     * 删除消息
     */
    @Log(title = "消息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(vitimMessageService.deleteVitimMessageByIds(ids));
    }
}
